# -*- encoding: utf-8 -*-
'''
file       :decode_video_stream.py
Description:对视频流进行解码
Date       :2023/01/04 17:11:42
Author     :Josco
version    :python3.7.8
'''
from config import *
from public import *
from dynamics_demarcate_pix import dynamics_demarcate_pix
from hash_check import hash_check

def load_ori_and_shoot_pic(ori_pic_file_path):
    """
        desc: 根据原始图像存储路径和拍摄后的照片路径获取对应的照片
        param:
            ori_pic_file_path: 原始图像路径
            pic_file_path: 拍摄后的照片存储路径
    """
    ori_file_list = []  # 存储原始图像的路径
    # 获取原始图像路径下的所有图像
    for top, dirs, nondirs in os.walk(ori_pic_file_path):
        for item in nondirs:
            if item.split('.')[-1] == "jpeg":
                ori_file_list.append(os.path.join(top, item))
    return ori_file_list

class check_cam_data(object):
    """读取相机视频流数据, 并根据hash运算得出动态确定视频流位置是否正确"""

    def __init__(self, dynamics_matrix) -> None:
        self.dynamics_matrix = dynamics_matrix

    # def read_cam_data(self, cap, _camera):
    #     """读取相机视频流"""
    #     while True:
    #         ret, _img = cap.read()

    #         for i in range(3):
    #             start_x, stop_x, start_y, stop_y = _camera[i]
    #             lp3 = _img[start_x:stop_x, start_y:stop_y]
    #             # print("_img.shape", _img.shape)
    #             # print("lp3.shape", lp3.shape)
    #             dynamics_point_matrix = self.dynamics_matrix[i]
    #             _r, _g, _b = fin_color_list[i]

    #             all_row_col = get_matrix_all_row_col(dynamics_point_matrix, i)
    #             # 用来存储所有matrix并用于计算均值的矩阵
    #             sum_matrix = np.zeros((dynamics_point_matrix.shape[0], 3))
    #             for _row_col in all_row_col:
    #                 # print("_row_col", _row_col)
    #                 final_point_color_matrix = lp3[_row_col[0], _row_col[1]]
    #                 sum_matrix += final_point_color_matrix
    #             sum_matrix = sum_matrix / len(all_row_col)
    #             sum_matrix = sum_matrix.reshape(
    #                 (-1, int(height/3), 3))

    #             final_sum_matrix = color_zero_to_one(sum_matrix, _r, _g, _b)
    #             final_sum_matrix = final_sum_matrix.astype("uint8")

    #             check_info = final_sum_matrix[:4]

    #             print("check_info.shape", check_info.shape)
    #             check_info = check_info.reshape(-1,)
    #             print(check_info)

    def read_cam_data(self, pic_name, _camera):
        """读取相机视频流"""
        # while True:
        # ret, _img = cap.read()
        _img = cv.imread(pic_name)
        do_hash_check = hash_check()
        for i in range(3):
            start_x, stop_x, start_y, stop_y = _camera[i]
            lp3 = _img[start_x:stop_x, start_y:stop_y]
            # print("_img.shape", _img.shape)
            # print("lp3.shape", lp3.shape)
            dynamics_point_matrix = self.dynamics_matrix[i]
            _r, _g, _b = fin_color_list[i]

            all_row_col = get_matrix_all_row_col(dynamics_point_matrix, i)
            # 用来存储所有matrix并用于计算均值的矩阵
            sum_matrix = np.zeros((dynamics_point_matrix.shape[0], 3))
            for _row_col in all_row_col:
                    # print("_row_col", _row_col)
                final_point_color_matrix = lp3[_row_col[0], _row_col[1]]
                sum_matrix += final_point_color_matrix
            sum_matrix = sum_matrix / len(all_row_col)
            sum_matrix = sum_matrix.reshape(
                    (-1, int(height/3), 3))

            final_sum_matrix = color_to_zero_one(sum_matrix, _r, _g, _b)
            final_sum_matrix = final_sum_matrix.astype("uint8")
            
            final_sum_matrix = np.rot90(final_sum_matrix, -1)  # 矩阵右转90° 
            check_info = final_sum_matrix[:4]
            # print("final_sum_matrix.shape", final_sum_matrix.shape)

            
            check_info = check_info.reshape(-1)
            check_info = check_info.astype("str")
            check_info_data = "".join(check_info)

            if "00110001001100100011001100110100001100110011001000110001" in check_info_data:
                # print("pic_name: ", pic_name)
                check_info_data = check_info_data.split("00110001001100100011001100110100001100110011001000110001")
                check_info_data = check_info_data[0]

                check_info_data = do_hash_check.bitarray2str(check_info_data)
                check_info_data = check_info_data.split(" ") # 按空格切割校验信息
                print(check_info_data)
                return pic_name, check_info_data[-3]
            return None, None
    
    def check_cam_data(self,pic_list, camera = camera_2):
        checked_list = []
        pic_num_list = []
        for pic_name in pic_list:
            read_cam_data_return, check_info_data = self.read_cam_data(pic_name, camera_2)
            if read_cam_data_return:
                checked_list.append(read_cam_data_return)
                pic_num_list.append(check_info_data)
        print("len(checked_list):", len(checked_list))
        pic_num_list = list(set(pic_num_list))

        # print(pic_num_list)
        print("len(pic_num_list):", len(pic_num_list))





def main():
    # 基于标准的图像和标准拍摄后的图像调用动态获取点位矩阵函数获取每日的动态点位矩阵
    camera = camera_2
    do_dynamics_demarcate_pix = dynamics_demarcate_pix()
    all_dynamics_point_matrix = do_dynamics_demarcate_pix.turn_pic(
        standard_pic, standard_ori_pic, camera)  # 获取到动态点位的矩阵

    # #进行摄像头的视频流初始化
    # cap = cv.VideoCapture(1)
    # cap.set(3,cam_width) # 初始化相机宽度
    # cap.set(4,cam_height) # 初始化相机高度
    # cap.set(5,cam_FPS) # 初始化相机的FPS

    pic_list = load_ori_and_shoot_pic("./shiping/")
    # print("pic_list", pic_list)
    print("len(pic_list)", len(pic_list))

    # 进行摄像头视频流的验证
    do_check_cam_data = check_cam_data(all_dynamics_point_matrix)

    do_check_cam_data.check_cam_data(pic_list, camera)


if __name__ == '__main__':
    main()
